<?php

    global $mikron;
    $mikron->Page->StartBody();
    
    $mikron->Tools->SiteMenu->setCurrentPage('/');
    $success_format = '<div class="success_box">%s</div>';
    $error_format = '<div class="error_box">%s</div>';

    $tab_page = new html_table(2, 'tosmr_tableform_class', '100%', 0, 0, null);
    $tab_page->newcol(null, false, 'left', null, null, null, 'top');
        echo '<h1>Регистрация</h1>';
        if(isset($_GET['ok']))
        {
            $success_text = $mikron->getVariable('REGISTRATION_SUCCESS');
            echo sprintf($success_format, $success_text);
            $tab_page->end();
            $mikron->Page->EndBody();
            return;            
        }
        elseif(isset($_GET['err']))
        {
            $error_text = $mikron->getVariable('REGISTRATION_FAIL');
            echo sprintf($success_format, $error_text);
            $tab_page->end();
            $mikron->Page->EndBody();
            return;            
        }

        $form = new html_form('enter_form');
        $email = null;
        if($form->pageindex == 2)
        {
            if(array_key_exists('email', $form->formdata))
            {
                $im_customer = array_key_exists('im_customer', $form->formdata);
                $im_performer = array_key_exists('im_performer', $form->formdata);
                $email = $form->formdata['email'];
                if($im_customer || $im_performer)
                {
                    $incorrect_email = $mikron->Tools->IsNullOrEmpty($email);
                    if($incorrect_email)
                    {
                            $error_text_0 = 'Некорректный адрес электронной почты.';
                            echo sprintf($error_format, $error_text_0);
                    }
                    else
                    {
                        // $mikron->ACL->IterateLimitedOperation('registration');
                        $registration_mail_subject = 'Регистрация на сайте tosmr.ru';
                        $subject = $registration_mail_subject;
                        $sender = 'support@tosmr.ru';
                        $password = $mikron->Tools->GeneratePassword(6, 9);

                        $user = new T_USER();
                        $user->login->value = $email;
                        $user->email->value = $email;
                        $user->password->value = $password;
                        if($im_customer)
                        {
                            $user->usertype->value = 1;
                        }
                        elseif($im_performer)
                        {
                            $user->usertype->value = 2;
                        }
                        try
                        {
                            $user->Save();
                            $userRole = $mikron->ACL->addRoleToUser($user, $im_customer ? 'customer' : 'performer', 99999);
                            $userRole->Save();
                            $message = $mikron->getVariable('REGISTRATION_MAIL');
                            $message = str_replace('%login%', $email, $message);
                            $message = str_replace('%password%', $password, $message);
                            $mail = new MikronMail($subject, $sender, $email, $message);
                            try
                            {
                                ob_start();
                                $mailingDone = $mail->Send();
                                if(!$mailingDone)
                                {
                                    throw new Exception('Не удалось отправить письмо.');
                                }
                                ob_get_clean();
                                $mikron->Tools->GoToPage($mikron->Tools->MakePath('?ok='.$email));
                            }
                            catch(Exception $ex)
                            {
                                ob_get_clean();
                                $mikron->Tools->GoToPage($mikron->Tools->MakePath('?err='.$email));
                                $error_text_1 = 'Не удалось отправить письмо.
                                В ближайшее время это будет сделано.';
                                echo sprintf($error_format, $error_text_1);
                            }
                        }
                        catch(Exception $ex)               
                        {
                            if(mysql_errno() == 1062)
                            {
                                $error_text_2 = 'Указанный Вами адрес уже используется на проекте.';
                                echo sprintf($error_format, $error_text_2);
                            }
                            else
                            {
                                $error_text_3 = 'Неизвестная ошибка. Пожалуйста попробуйте еще раз.';
                                echo sprintf($error_format, $error_text_3);
                            }
                        }
                    }
                }
            }
        }
        $form->pageindex = 1;
        $tab0 = new html_table(2, 'tosmr_big_form', 120, 0, 0);
            $tab0->newcol('E-mail', true, false, null, null, null, null, null, 2);
            $tab0->newcol(null, true, false, null, null, null, null, null, 2);
                $form->addInput(1, 'email', null, 40, null, false, $email, false);
            $tab0->newcol(null, true, false, null, null, null, null, null, 1);
                $form->addSubmit('im_customer', 'Я заказчик');
            $tab0->newcol(null, true, 'right', null, null, null, null, null, 1);
                $form->addSubmit('im_performer', 'Я исполнитель');
            $tab0->end();
        $form->end();
    $tab_page->newcol(null, false, 'left', null, null, null, 'top');
        echo '{{$REGISTRATION_INFO}}';
    $tab_page->end();

$mikron->Page->EndBody();
?>